#include "widget.h"
#include "ui_widget.h"



Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowTitle("client");
    //m_tabWidget = ui->tabWidget;
    //m_page1 = new Page1(m_tabWidget->widget(0)); //只传入对应的一个页面

    m_stackedWidget = nullptr;
    m_btnGroup = nullptr;

    m_DataShowPageWidget = new DataShowPageWidget();
    m_CustomControlPageWidget = new CustomControlPageWidget();

    initMain();
}

Widget::~Widget()
{
    if(m_DataShowPageWidget)
    {
        delete m_DataShowPageWidget;
        m_DataShowPageWidget = nullptr;
    }
    if(m_CustomControlPageWidget)
    {
        delete m_CustomControlPageWidget;
        m_CustomControlPageWidget = nullptr;
    }
    if(m_stackedWidget)
    {
        delete m_stackedWidget;
        m_stackedWidget = nullptr;
    }
    if(m_btnGroup)
    {
        delete m_btnGroup;
        m_btnGroup = nullptr;
    }
    delete ui;
}

void Widget::initMain()
{
     qDebug() << "111:" ;
    //主窗口水平布局 分为左右
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(0); //间距
    mainLayout->setContentsMargins(0,0,0,0);

    m_leftWdiget = new QWidget(this);
    m_leftWdiget->setMaximumWidth(80);
    m_leftWdiget->setObjectName("leftWdiget");
    m_leftWdiget->setStyleSheet("background-color: #BFEFFF");

    //左窗口做网格布局
    QGridLayout * gridLayout = new QGridLayout(m_leftWdiget);
    gridLayout->setSpacing(10);
    gridLayout->setContentsMargins(8,16,8,8);

    mainLayout->addWidget(m_leftWdiget);

    //右窗口放 堆栈窗口
    m_rightWdiget = new QWidget(this);
    m_rightWdiget->setObjectName("rightWdiget");
    m_rightWdiget->setStyleSheet("background-color: #E0FFFF");

    //垂直布局
    QVBoxLayout* vboxLayout = new QVBoxLayout(m_rightWdiget);
    vboxLayout->setSpacing(0); //间距
    vboxLayout->setContentsMargins(0,0,0,0);

    // 创建堆栈窗口
    m_stackedWidget = new QStackedWidget(m_rightWdiget);
    //m_stackedWidget = ui->stackedWidget;
    vboxLayout->addWidget(m_stackedWidget);

    mainLayout->addWidget(m_rightWdiget);

    // 添加页面
    m_stackedWidget->addWidget(m_DataShowPageWidget);
    m_stackedWidget->addWidget(m_CustomControlPageWidget);

    //相当于弹簧效果
    QGridLayout * leftLayout2 = (QGridLayout*)m_leftWdiget->layout();
    leftLayout2->setRowStretch(5,5);

    //添加按钮
    InitLeftMune();

}

void Widget::InitLeftMune()
{
    //读取配置文件 也页面列表的数据 名称
    QString strcfgpath = QCoreApplication::applicationDirPath();
    qDebug() << "本地路径:" << strcfgpath;
    strcfgpath += "/config/menucfg.json";
    QJsonDocument jsdocmenulist;
    loadJsonConfig(strcfgpath, jsdocmenulist);

    if (!jsdocmenulist.isArray())
    {
        qWarning() << "配置文件格式错误：根元素不是数组";
        return;
    }

    //创建 按钮组
    m_btnGroup = new QButtonGroup(this);
    //遍历数组
    const QJsonArray & jsmenuarr = jsdocmenulist.array();
    for(int i = 0; i < jsmenuarr.size(); ++i)
    {
        QJsonValue value = jsmenuarr.at(i);
        if(value.isString())
        {
            qDebug() << "字符串:" << value.toString();
        }
        else if(value.isObject())
        {
            QJsonObject jstemp = value.toObject();
            QString strname = jstemp["name"].toString();
            QString strcode = jstemp["code"].toString();

            QToolButton *btn1 = new QToolButton();
            btn1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
            btn1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            btn1->setText(strname);
            //QPushButton *btn1 = new QPushButton(strname, this);


            //btn1->setIcon(QIcon("D:/testQt/QtClient/MainApp/images/menu/datadisplay.png"));
            QString strimg = ":/images/menu/";
            strimg += strcode;
            strimg += ".png";

            btn1->setIcon(QIcon(strimg));  // 冒号开头表示资源路径
            btn1->setIconSize(QSize(32, 32));

            btn1->setFixedSize(64, 64);              // 强制固定尺寸
            btn1->setCheckable(true);

            btn1->setStyleSheet(
                R"(
                    QToolButton{
                        border-style: none;
                        border:1px solid #C0DCF2;
                        font:12px "Microsoft YaHei";
                        color:#00BFFF;
                        padding:5px;
                        border-radius:5px;
                        background:#DEF0FE;
                    }
                    QToolButton:hover{
                        background:#B0E2FF;
                    }
                    QToolButton:checked{
                        background:#B0E2FF;
                    }

                )"
                );


            // 添加按钮到组并设置ID
            m_btnGroup->addButton(btn1, i);

            //添加找到左窗口layout
            QGridLayout * leftLayout = (QGridLayout*)m_leftWdiget->layout();
            leftLayout->addWidget(btn1, i, 1);

            //连接槽函数
            connect(m_btnGroup, QOverload<int>::of(&QButtonGroup::idClicked), this, &Widget::OnBtnGroupClicked);
        }

    }

    qDebug() << "333:" ;
    //设置单选模式（默认互斥）
    m_btnGroup->setExclusive(true);
    //默认选中第一个
    m_btnGroup->button(0)->click();
}

void Widget::OnBtnGroupClicked(int id)
{
    m_stackedWidget->setCurrentIndex(id);
}

void Widget::loadJsonConfig(const QString &filePath, QJsonDocument  &jsdocResult)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning() << "无法打开文件:" << filePath;
        return;
    }

    QByteArray jsonData = file.readAll();
    file.close();

    qWarning() << "JSON:" << jsonData.toStdString();

    QJsonParseError parseError;
    QJsonDocument doc = QJsonDocument::fromJson(jsonData, &parseError);
    if (parseError.error != QJsonParseError::NoError) {
        qWarning() << "JSON解析错误:" << parseError.errorString();
        return;
    }
    qWarning() << "JSON获取完成" << doc.toJson().toStdString();
    jsdocResult = doc;
}
